package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.LocalIPProtocolVerAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * <Description> <br>
 *
 * @author cheng.leizhen01<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月16日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler <br>
 */
@Service
@AttrHandler(LocalIPProtocolVerAttrHandler.class)
public class LocalIPProtocolVerAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    /**
     *当IP协议版本选择IPv4或V4/IPV6双栈时，客户申请IPv4地址数量显示且必填，否则隐藏，若同时ip地址来源选择客户自带则属性非必填
     * 当IP协议版本选择IPv6或V4/IPV6双栈时，客户申请IPv6地址数量显示且必填，否则隐藏。若若同时ip地址来源选择客户自带则属性非必填
     * 当IP协议版本选择IPv4或V4/IPV6双栈时，IPv4地址显示非必填，否则隐藏。若同时ip地址来源选择客户自带则属性必填
     * 当IP协议版本选择IPv6或V4/IPV6双栈时，IPv6地址显示非必填，否则隐藏，若同时ip地址来源选择客户自带则属性必填
     */
    @Resource
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        //获取登录员工的省份信息
        String opreatorProvinceNbr= ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        if (Objects.isNull(attr)) {
            return;
        }
        //IP地址来源
        KiteAttr IPAddressSource = inst.getAttr("2_0000060");
        //客户申请IPv4地址数量
        KiteAttr IPv4AddressesNumber = inst.getAttr("2_0000193");   //客户申请IPv4地址数量
        // 客户申请IPv6地址数量
        KiteAttr IPv6AddressesNumber = inst.getAttr("2_0000194");   //客户申请IPv4地址数量
        // IPv4地址
        KiteAttr IPv4Addresses = inst.getAttr("2_0000202");
        //IPv6地址
        KiteAttr IPv6Addresses = inst.getAttr("2_0000203");
        //IPV6掩码长度
        KiteAttr IPV6MaskLength = inst.getAttr("200002031");

        //IP协议版本选择IPV6或IPV4/IPV6双栈时联动展示且必填
        if(Objects.nonNull(IPV6MaskLength) && KiteStringUtils.equalsAny(attr.getValue(),"1_0000220","1_0000221")){
            IPV6MaskLength.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
            IPV6MaskLength.setIsNull(KeyConsts.IFTRUE_F);
        }else {
            IPV6MaskLength.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
        }

        if (Objects.isNull(IPAddressSource) || Objects.isNull(IPv4AddressesNumber)|| Objects.isNull(IPv6AddressesNumber)
            || Objects.isNull(IPv4Addresses) || Objects.isNull(IPv6Addresses)) {
            return;
        }
        Map<String, Object> loginExtParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        String sceneInitParam = KiteMapUtils.getStrValue(loginExtParams, "upServiceOfferId");
        String upServiceOfferId = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "upServiceOfferId");
        boolean modifyChaseOrder = LocalServiceOfferConsts.SERVICE_OFFER_FOLLOW.equals(data.getServiceOfferId())
            && (KiteStringUtils.isNotEmpty(sceneInitParam) || KiteStringUtils.isNotEmpty(upServiceOfferId));

        if(!modifyChaseOrder){
            if(KiteStringUtils.equalsAny(attr.getValue(),"1_0000219" )){
                //当IP协议版本选择IPv4或V4/IPV6双栈时，显示且必填，否则隐藏，若同时ip地址来源选择客户自带则属性非必填
                IPv4AddressesNumber.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                IPv6AddressesNumber.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                //IP地址
                IPv4Addresses.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                IPv6Addresses.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                IPv6Addresses.setValue("");  //兼容跨域工单的判断
                IPv6Addresses.setValueDesc("");

                if(KiteStringUtils.equalsAny(IPAddressSource.getValue(),"1_0000258") || (KiteStringUtils.isEmpty(IPAddressSource.getValue()))){
                    // 1_0000258 联通分配
                    IPv4AddressesNumber.setIsNull(KeyConsts.IFTRUE_F);
                    IPv4Addresses.setIsNull(KeyConsts.IFTRUE_T);
                }else {
                    IPv4AddressesNumber.setIsNull(KeyConsts.IFTRUE_T);
                    IPv4Addresses.setIsNull(KeyConsts.IFTRUE_F);
                }
            } else if(KiteStringUtils.equalsAny(attr.getValue(),"1_0000220" )) {
                //当IP协议版本选择IPv6或V4/IPV6双栈时，显示且必填，否则隐藏。若同时ip地址来源选择客户自带则属性非必填
                IPv6AddressesNumber.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                IPv4AddressesNumber.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                //IP地址
                IPv6Addresses.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                IPv4Addresses.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);

                if (KiteStringUtils.equalsAny(IPAddressSource.getValue(), "1_0000258")) {
                    IPv6AddressesNumber.setIsNull(KeyConsts.IFTRUE_F);
                    IPv6Addresses.setIsNull(KeyConsts.IFTRUE_T);
                } else {
                    IPv6AddressesNumber.setIsNull(KeyConsts.IFTRUE_T);
                    IPv6Addresses.setIsNull(KeyConsts.IFTRUE_F);
                }
            }else if(KiteStringUtils.equalsAny(attr.getValue(),"1_0000221")){
                //IP数量
                IPv6AddressesNumber.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                IPv4AddressesNumber.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                //IP地址
                IPv4Addresses.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                IPv6Addresses.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);

                if (KiteStringUtils.equalsAny(IPAddressSource.getValue(), "1_0000258")) {
                    IPv4AddressesNumber.setIsNull(KeyConsts.IFTRUE_F);
                    IPv6AddressesNumber.setIsNull(KeyConsts.IFTRUE_F);
                    IPv4Addresses.setIsNull(KeyConsts.IFTRUE_T);
                    IPv6Addresses.setIsNull(KeyConsts.IFTRUE_T);
                } else {
                    IPv4AddressesNumber.setIsNull(KeyConsts.IFTRUE_T);
                    IPv6AddressesNumber.setIsNull(KeyConsts.IFTRUE_T);
                    IPv4Addresses.setIsNull(KeyConsts.IFTRUE_F);
                    IPv6Addresses.setIsNull(KeyConsts.IFTRUE_F);
                }
            }
        }


        //IP地址业务变更场景
        //IPV4IP变更类型
        KiteAttr IPv4IPChageType = inst.getAttr("210004178");
        //IPV6IP变更类型
        KiteAttr IPv6IPChageType = inst.getAttr("210004179");

        if((KiteStringUtils.equals(data.getServiceOfferId(),"2826") || modifyChaseOrder)
            && !Objects.isNull(IPv4IPChageType) && !Objects.isNull(IPv6IPChageType)){
            KiteAttr IPV6Length = inst.getAttr("200002031");   // IPV6掩码长度   1_0000219 IPv4
            if(!Objects.isNull(IPv4IPChageType.getValue()) && KiteStringUtils.equalsAny(attr.getValue(),"1_0000219" )) {
                //当IP协议版本 选择IPv4
                LocalAttrHelper.showAndNotNull(inst, "210004178");
                if("1_0000258".equals(IPAddressSource.getValue())){   //联通分配
                    if("110007223".equals(IPv4IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004180");
                        LocalAttrHelper.hideAndNullable(inst, "200002031","210004179","210004181","210004182",
                            "210004183","210004184","210004185","210004186","210004187");

                    } else if("110007224".equals(IPv4IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004182","210004184");
                        LocalAttrHelper.hideAndNullable(inst, "200002031","210004179","210004181",
                            "210004183","210004185","210004186","210004187","210004180");

                    } else if("100007396".equals(IPv4IPChageType.getValue())){  //不变
                        LocalAttrHelper.hideAndNullable(inst, "200002031","210004179","210004181",
                            "210004183","210004185","210004186","210004187","210004180","210004182","210004184");

                    }
                }else if("1_0000259".equals(IPAddressSource.getValue())){  // 客户自带
                    if("110007223".equals(IPv4IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004180","210004186");
                        LocalAttrHelper.hideAndNullable(inst, "200002031","210004179","210004181","210004182",
                            "210004183","210004184","210004185","210004187");

                    } else if("110007224".equals(IPv4IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004182","210004184");
                        LocalAttrHelper.hideAndNullable(inst, "200002031","210004179","210004181",
                            "210004183","210004185","210004186","210004187","210004180");

                    } else if("100007396".equals(IPv4IPChageType.getValue())) {  //不变
                        LocalAttrHelper.hideAndNullable(inst, "200002031","210004179","210004181",
                            "210004183","210004185","210004186","210004187","210004180","210004182","210004184");
                    }
                }

                // IP协议版本选择 IPV6
            } else if (!Objects.isNull(IPv6IPChageType.getValue()) && KiteStringUtils.equalsAny(attr.getValue(),"1_0000220" )) {
                //当IP协议版本选择IPv6，展示IPv6变更类型
                IPV6Length.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                LocalAttrHelper.showAndNotNull(inst, "210004179");
                if("1_0000258".equals(IPAddressSource.getValue())){   //联通分配
                    if("110007223".equals(IPv6IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004181");
                        LocalAttrHelper.hideAndNullable(inst, "210004182","210004180",
                            "210004183","210004184","210004185","210004186","210004187","210004178");

                    } else if("110007224".equals(IPv6IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004183","210004185");
                        LocalAttrHelper.hideAndNullable(inst, "","210004181","210004182","210004184",
                            "210004186","210004187","210004180","210004178");

                    } else if("100007396".equals(IPv6IPChageType.getValue())){  //不变
                        LocalAttrHelper.hideAndNullable(inst, "210004181",
                            "210004183","210004185","210004186","210004187","210004180","210004182","210004184","210004178");

                    }
                }else if("1_0000259".equals(IPAddressSource.getValue())){  // 客户自带
                    if("110007223".equals(IPv6IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004181","210004187");
                        LocalAttrHelper.hideAndNullable(inst, "210004178","210004180","210004182",
                            "210004183","210004184","210004185","210004186");

                    } else if("110007224".equals(IPv6IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004183","210004185");
                        LocalAttrHelper.hideAndNullable(inst, "210004178","210004181","210004182",
                            "210004184","210004186","210004187","210004180");

                    } else if("100007396".equals(IPv6IPChageType.getValue())) {  //不变
                        LocalAttrHelper.hideAndNullable(inst, "210004178","210004180","210004181",
                            "210004182","210004183","210004184","210004185","210004186","210004187");
                    }
                }

            } else if ((!Objects.isNull(IPv4IPChageType.getValue()) || !Objects.isNull(IPv6IPChageType.getValue()))&&KiteStringUtils.equalsAny(attr.getValue(),"1_0000221")) {
                IPV6Length.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                LocalAttrHelper.showAndNotNull(inst, "210004178","210004179");
                // IPV4版本协议
                if("1_0000258".equals(IPAddressSource.getValue())){   //联通分配
                    if("110007223".equals(IPv4IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004180");
                        LocalAttrHelper.hideAndNullable(inst, "210004182","210004184","210004186");

                    } else if("110007224".equals(IPv4IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004182","210004184");
                        LocalAttrHelper.hideAndNullable(inst, "210004180","210004186");

                    } else if("100007396".equals(IPv4IPChageType.getValue())){  //不变
                        LocalAttrHelper.hideAndNullable(inst, "210004180","210004182","210004184","210004186");

                    }
                }else if("1_0000259".equals(IPAddressSource.getValue())){  // 客户自带
                    if("110007223".equals(IPv4IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004180","210004186");
                        LocalAttrHelper.hideAndNullable(inst, "210004182","210004184");

                    } else if("110007224".equals(IPv4IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004182","210004184");
                        LocalAttrHelper.hideAndNullable(inst, "210004180","210004186");

                    } else if("100007396".equals(IPv4IPChageType.getValue())) {  //不变
                        LocalAttrHelper.hideAndNullable(inst, "210004180","210004182","210004184", "210004186");
                    }
                }

                // IPV6版本协议
                if("1_0000258".equals(IPAddressSource.getValue())){   //联通分配
                    if("110007223".equals(IPv6IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004181");
                        LocalAttrHelper.hideAndNullable(inst, "210004183","210004185", "210004187");

                    } else if("110007224".equals(IPv6IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004183","210004185");
                        LocalAttrHelper.hideAndNullable(inst, "","210004181","210004187");

                    } else if("100007396".equals(IPv6IPChageType.getValue())){  //不变
                        LocalAttrHelper.hideAndNullable(inst, "210004181","210004183","210004185", "210004187");

                    }
                }else if("1_0000259".equals(IPAddressSource.getValue())){  // 客户自带
                    if("110007223".equals(IPv6IPChageType.getValue())){        //增加
                        LocalAttrHelper.showAndNotNull(inst, "210004181","210004187");
                        LocalAttrHelper.hideAndNullable(inst, "210004183","210004185");

                    } else if("110007224".equals(IPv6IPChageType.getValue())){  //减少
                        LocalAttrHelper.showAndNotNull(inst, "210004183","210004185");
                        LocalAttrHelper.hideAndNullable(inst, "210004181","210004187");

                    } else if("100007396".equals(IPv6IPChageType.getValue())) {  //不变
                        LocalAttrHelper.hideAndNullable(inst, "210004181","210004183","210004185", "210004187");
                    }
                }
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

}
